package groupon.common.util;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class AppConfiguration {

	private static final Logger logger = LoggerFactory
			.getLogger(AppConfiguration.class);

	private static final String APP_PROPS_FILE = "META-INF/apps.properties";
	private static final String INIT_DATA_INSTALL = "groupon.initdata.install";
	private static final String EMAIL_SYSTEM = "groupon.email.system";
	private static final String APP_CONTEXT = "groupon.deploy.context";
	
	private static PropertiesConfiguration propsConfig;

	private AppConfiguration() {
	}

	static {
		try {
			propsConfig = new PropertiesConfiguration(APP_PROPS_FILE);
			propsConfig.setAutoSave(true);
			propsConfig
					.setReloadingStrategy(new FileChangedReloadingStrategy());
		} catch (Exception e) {
			logger.warn("Could not load configuration file from: "
					+ APP_PROPS_FILE, e);
		}
	}
	
	public static Boolean getBoolean(String key, Boolean defaultValue) {
	        Boolean value = propsConfig.getBoolean(key, null);
	        if (value != null) {
	            return value;
	        }
	        return defaultValue;
    }
	 
	public static boolean isInitDataInstall(){
		return getBoolean(INIT_DATA_INSTALL, false);
	}
	
	public static String getString(String key, String defaultValue){
		String value = propsConfig.getString(key, null);
		if(value != null){
			return value;
		}
		return defaultValue;
	}
	
	/*get email system*/
	public static String getEmailSystem(){
		return getString(EMAIL_SYSTEM, null);
	}
	
	public static String getAppContext() {
		return getString(APP_CONTEXT, "");
	}
	
}
